<?php
function generateItem($menu,$oldOrder,$item,$item_id=null,$pref='',$suf=''){
    if($item_id>0){
        $prefix = $pref.'[items]['.$oldOrder.']';
        $sufix = $suf.'-item-'.$oldOrder;
    }else{
        $prefix = $pref;
        $sufix = $suf;
    }
    
    /*modifier item existant*/
    $html = '<li class="ui-state-default">';
    $html .= "\t".'Nom : <input type="text" name="'.$prefix.'[label]" value="'.$item['label'].'"/>'."\n";
    if(count(PWE::getLanguesActives())>0){
        $html .= '<span class="langue">';
        foreach(PWE::getLanguesActives() as $lang){
            $html .= '<span class="'.$lang.'" onclick="javascript:openTranslate(\''.$lang.'\',\'menu_item\',\''.$item['id'].'\',\'modele_label\',\'input\');">&nbsp;</span>';
        }
        $html .= '</span>'."\n\r";
    }
    $html .= "\t".'Titre : <input type="text" name="'.$prefix.'[title]" value="'.$item['title'].'"/>'."\n";
    if(count(PWE::getLanguesActives())>0){
        $html .= '<span class="langue">';
        foreach(PWE::getLanguesActives() as $lang){
            $html .= '<span class="'.$lang.'" onclick="javascript:openTranslate(\''.$lang.'\',\'menu_item\',\''.$item['id'].'\',\'modele_title\',\'input\');">&nbsp;</span>';
        }
        $html .= '</span>'."\n\r";
    }
    $html .= "\t".'Lien : <input id="link-menu-'.$menu->getId().$sufix.'" type="text" name="'.$prefix.'[link]" value="'.$item['link'].'"/><div class="ui-state-default ui-corner-all container-btn-tool" onclick="javascript:openDialog(\'findPermalien\',\'link-menu-'.$menu->getId().$sufix.'\');"><span class="ui-icon ui-icon-gear">select</span></div>'."\n";
    if(count(PWE::getLanguesActives())>0){
        $html .= '<span class="langue">';
        foreach(PWE::getLanguesActives() as $lang){
            $html .= '<span class="'.$lang.'" onclick="javascript:openTranslate(\''.$lang.'\',\'menu_item\',\''.$item['id'].'\',\'modele_link\',\'input\');">&nbsp;</span>';
        }
        $html .= '</span>'."\n\r";
    }
    $html .= "\t".'Cible : <select name="'.$prefix.'[target]">'."\n";
    $html .= "\t"."\t".'<option value="_top"';
    if($item['target']==='_top') $html .= ' selected="selected" ';
    $html .= '>page courante</option>'."\n";
    $html .= "\t"."\t".'<option value="_blank" ';
    if($item['target']==='_blank') $html .= ' selected="selected" ';
    $html .= ' >nouvelle page</option>'."\n";
    $html .= "\t".'</select>'."\n";
    $html .= "\t".'Style : <input type="text" name="'.$prefix.'[class]" value="'.$item['class'].'"/>'."\n";
    $html .= "\t".'<input type="hidden" name="'.$prefix.'[menu_id]" value="'.$menu->getId().'"/>'."\n";
    $html .= "\t".'<input type="hidden" name="'.$prefix.'[id]" value="'.$item['id'].'"/>'."\n";
    $html .= "\t".'<input id="link-menu-'.$menu->getId().$sufix.'_modele" type="hidden" name="'.$prefix.'[modele]" value="'.$item['modele'].'"/>'."\n";
    $html .= "\t".'<input id="link-menu-'.$menu->getId().$sufix.'_modele_id" type="hidden" name="'.$prefix.'[modele_id]" value="'.$item['modele_id'].'"/>'."\n";
    $html .= "\t".'<input type="hidden" name="'.$prefix.'[menu_item_id]" value="'.$item_id.'"/>'."\n";
    $html .= "\t".'<input class="orderValue" type="hidden" name="'.$prefix.'[order]" value="'.$oldOrder.'"/>'."\n";
    $html .= "\t".'<input type="button" value="+" class="pwe-ui-btn" onclick="javascript:toggleAddItem(\'addItem-menu-'.$menu->getId().$sufix.'-item-'.count($item['items']).'\');"/>'."\n";
    $html .= "\t".'<ul class="liste-items-menu">'."\n";
    $order = 0;
    if(count($item['items'])>0){
        foreach($item['items'] as $sorder=>$sitem){
            $html .= generateItem($menu, $sorder, $sitem,$item['id'],$prefix,$sufix);
            $order++;
        }
    }
    /* ajouter un niveau inferieur */
    $html .= '<li class="ui-state-default" style="display: none;" id="addItem-menu-'.$menu->getId().$sufix.'-item-'.$order.'">';
    $html .= "\t".'Nom : <input type="text" name="'.$prefix.'[items]['.$order.'][label]" value=""/>'."\n";
    $html .= "\t".'Titre : <input type="text" name="'.$prefix.'[items]['.$order.'][title]" value=""/>'."\n";
    $html .= "\t".'Lien : <input id="link-menu-'.$menu->getId().$sufix.'-item-'.$order.'" type="text" name="'.$prefix.'[items]['.$order.'][link]" value=""/><div class="ui-state-default ui-corner-all container-btn-tool" onclick="javascript:openDialog(\'findPermalien\',\'link-menu-'.$menu->getId().$sufix.'-item-'.$order.'\');"><span class="ui-icon ui-icon-gear">select</span></div>'."\n";
    $html .= "\t".'Cible : <select name="'.$prefix.'[items]['.$order.'][target]">'."\n";
    $html .= "\t"."\t".'<option value="_top">page courante</option>'."\n";
    $html .= "\t"."\t".'<option value="_blank">nouvelle page</option>'."\n";
    $html .= "\t".'</select>'."\n";
    $html .= "\t".'Style : <input type="text" name="'.$prefix.'[items]['.$order.'][class]" value=""/>'."\n";
    $html .= "\t".'<input type="hidden" name="'.$prefix.'[items]['.$order.'][menu_id]" value="'.$menu->getId().'"/>'."\n";
    $html .= "\t".'<input type="hidden" name="'.$prefix.'[items]['.$order.'][id]" value=""/>'."\n";
    $html .= "\t".'<input type="hidden" id="link-menu-'.$menu->getId().$sufix.'-item-'.$order.'_modele" name="'.$prefix.'[items]['.$order.'][modele]" value=""/>'."\n";
    $html .= "\t".'<input type="hidden" id="link-menu-'.$menu->getId().$sufix.'-item-'.$order.'_modele_id" name="'.$prefix.'[items]['.$order.'][modele_id]" value="0"/>'."\n";
    $html .= "\t".'<input type="hidden" name="'.$prefix.'[items]['.$order.'][menu_item_id]" value="'.$item['id'].'"/>'."\n";
    $html .= "\t".'<input class="orderValue" type="hidden" name="'.$prefix.'[items]['.$order.'][order]" value="'.$order.'"/>'."\n";
    $html .= '</li>'."\n";
    
    $html .= "\t".'</ul>'."\n";
    $html .= '</li>'."\n";
    
    return $html;
}
?>

<?php $listeMenus = modele_menu::getListe(); ?>
<div class="columns sixteen">
    <h1>Gestion des menus de navigation</h1>
    <div id="tabs-menu">
        <ul>
            <?php if(count($listeMenus)>0): ?>
            <?php foreach($listeMenus as $menu): ?>
            <li><a href="#menu-<?php echo $menu->getId(); ?>"><?php echo $menu->getIdentifiant(); ?></a></li>
            <?php endforeach; ?>
            <?php endif; ?>
            <li><a href="#menu-add"><b>+</b></a></li>
        </ul>
        <?php if(count($listeMenus)>0): ?>
        <?php foreach($listeMenus as $menu): ?>
        <div id="menu-<?php echo $menu->getId(); ?>">
            <form method="post" enctype="multipart/form-data" action="?template=menu&action=update&modele=menu&do=1&modele_id=<?php echo $menu->getId(); ?>">
                <?php $data = $menu->getDatas(); ?>
                nom  : <input class="identifiantMenu" id="identifiant-menu-<?php echo $menu->getId(); ?>" type="text" name="modele_identifiant" value="<?php echo $data['identifiant']; ?>" /><div class="ui-state-default ui-corner-all container-btn-tool" onclick="javascript:openDialog('targetSidebar','identifiant-menu-<?php echo $menu->getId(); ?>');"><span class="ui-icon ui-icon-gear"></span></div>
                <ul class="liste-items-menu">
                    <?php foreach( $data['items'] as $order=>$item ): ?>
                    <?php echo generateItem($menu, $order, $item,null,'modele_items['.$order.']','-item-'.$order); ?>
                    <?php endforeach; ?>
                    <li class="ui-state-default">
                        Nom : <input type="text" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][label]" value=""/>
                        Titre : <input type="text" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][title]" value=""/>
                        Lien : <input id="link-menu-<?php echo $menu->getId(); ?>-item-<?php echo (isset($order))?$order+1:1; ?>" type="text" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][link]" value=""/><div class="ui-state-default ui-corner-all container-btn-tool" onclick="javascript:openDialog('findPermalien','link-menu-<?php echo $menu->getId(); ?>-item-<?php echo (isset($order))?$order+1:1; ?>');"><span class="ui-icon ui-icon-gear">select</span></div>
                        Cible : <select name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][target]">
                            <option value="_top">page courante</option>
                            <option value="_blank">nouvelle page</option>
                        </select>
                        Style : <input type="text" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][class]" value=""/>
                        <input type="hidden" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][menu_id]" value="<?php echo $menu->getId(); ?>"/>
                        <input type="hidden" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][id]" value="0"/>
                        <input type="hidden" id="link-menu-<?php echo $menu->getId(); ?>-item-<?php echo (isset($order))?$order+1:1; ?>_modele" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][modele]" value=""/>
                        <input type="hidden" id="link-menu-<?php echo $menu->getId(); ?>-item-<?php echo (isset($order))?$order+1:1; ?>_modele_id"name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][modele_id]" value="0"/>
                        <input class="orderValue" type="hidden" name="modele_items[<?php echo (isset($order))?$order+1:1; ?>][order]" value="<?php echo (isset($order))?$order+1:1; ?>"/>
                    </li>
                </ul>
                <input type="submit" value="Enregistrer" class="pwe-ui-btn"/>
                <a href="?template=menu&action=delete&modele=menu&do=1&modele_id=<?php echo $menu->getId(); ?>"  class="pwe-ui-btn confirmAction" title="Supprimer le menu '<?php echo $data['identifiant']; ?>'">Supprimer</a>
                <input type="hidden" name="modele_id" value="<?php echo $menu->getId(); ?>" />
            </form>
        </div>
        <?php endforeach; ?>
        <?php endif; ?>
        <div id="menu-add">
            <form method="post" enctype="multipart/form-data" action="?template=menu&action=create&modele=menu&do=1">
                nom : <input class="identifiantMenu" id="identifiant-menu-0" type="text" name="modele_identifiant" value="" /><div class="ui-state-default ui-corner-all container-btn-tool" onclick="javascript:openDialog('targetSidebar','identifiant-menu-0');"><span class="ui-icon ui-icon-gear"></span></div>
                <input type="submit" value="Enregistrer" class="pwe-ui-btn"/>
            </form>
        </div>
    </div>
</div>

<!-- dialog box -->

<!-- définir un lien interne (page, catégorie, media -->
<div id="findPermalien" class="accordionListeContent" title="Liste des contenus">
    <h3>Rubriques</h3>
    <div>
        <ul class="selectableListe">
        <?php foreach(modele_category::getPrimaryListe() as $primary): ?>
            <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="category" modele_id="<?php echo $primary->getId(); ?>" permalien="<?php echo $primary->getPermalien()->getLien(); ?>"><?php echo $primary->getTitle(); ?></li>
            <?php if($primary->getNombrePublications()>0): ?>
            <ul>
            <?php foreach($primary->getPublications() as $publication): ?>
                <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="<?php echo ($publication->type==='page')?'page':'publication'; ?>" modele_id="<?php echo $publication->getId(); ?>" permalien="<?php echo $publication->getPermalien()->getLien(); ?>"><i><?php echo $publication->getPermalien()->getLien(); ?></i></li>
            <?php endforeach; ?>
            </ul>
            <?php endif; ?>
            <?php if($primary->haveChild()): ?>
            <ul>
            <?php foreach($primary->getChild() as $secondary): ?>
                <?php if(get_class($secondary) !== 'modele_page'): ?>
                <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="category" modele_id="<?php echo $secondary->getId(); ?>" permalien="<?php echo $secondary->getPermalien()->getLien(); ?>"><?php echo $secondary->getTitle(); ?></li>
                    <?php if($secondary->getNombrePublications()>0): ?>
                    <ul>
                    <?php foreach($secondary->getPublications() as $publication): ?>
                        <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');"  modele="<?php echo ($publication->type==='page')?'page':'publication'; ?>" modele_id="<?php echo $publication->getId(); ?>"  permalien="<?php echo $publication->getPermalien()->getLien(); ?>"><i><?php echo $publication->getPermalien()->getLien(); ?></i></li>
                    <?php endforeach; ?>
                    </ul>
                    <?php endif; ?>
                    <?php if($secondary->haveChild()): ?>
                    <ul>
                    <?php foreach($secondary->getChild() as $third): ?>
                        <?php if(get_class($third) !== 'modele_page'): ?>
                        <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');"  modele="category" modele_id="<?php echo $third->getId(); ?>" permalien="<?php echo $third->getPermalien()->getLien(); ?>"><?php echo $third->getTitle(); ?></li>
                            <?php if($third->getNombrePublications()>0): ?>
                            <ul>
                            <?php foreach($third->getPublications() as $publication): ?>
                                <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="<?php echo ($publication->type==='page')?'page':'publication'; ?>" modele_id="<?php echo $publication->getId(); ?>" permalien="<?php echo $publication->getPermalien()->getLien(); ?>"><i><?php echo $publication->getPermalien()->getLien(); ?></i></li>
                            <?php endforeach; ?>
                            </ul>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php endforeach; ?>
                    </ul>
                    <?php endif; ?>
                <?php endif; ?>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        <?php endforeach; ?>
        </ul>
    </div>
    <h3>Pages</h3>
    <div>
        <ul class="selectableListe">
        <?php foreach(modele_page::getListe() as $page): ?>
            <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="page" modele_id="<?php echo $page->getId(); ?>" permalien="<?php echo $page->getPermalien()->getLien(); ?>"><i><?php echo $page->getPermalien()->getLien(); ?></i></li>
        <?php endforeach; ?>
        </ul>
    </div>
    <h3>Galeries</h3>
    <div>
        <ul class="selectableListe">
            <?php foreach(modele_galerie::getListe() as $galerie): ?>
            <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="galerie" modele_id="<?php echo $galerie->getId(); ?>" permalien="<?php echo $galerie->getPermalien()->getLien(); ?>"><?php echo $galerie->getTitle(); ?></li>    
            <?php endforeach; ?>
        </ul>
    </div>
    <h3>Médias</h3>
    <div>
        <ul class="selectableListe">
            <?php foreach(modele_media::getListe() as $media): ?>
            <li onclick="javascript:getPermalienFromListe($(this),'findPermalien');" modele="media" modele_id="<?php echo $media->getId(); ?>" permalien="<?php echo PWE::getBaseURL().$media->getPath(); ?>">
                <?php if($media->getMediaTypeId()==1): ?>
                    <img src="<?php echo PWE::getBaseURL().$media->getPath(); ?>" height="50" title="<?php echo $media->getTitle(); ?>" alt="<?php echo $media->getTitle(); ?>"/>
                    <?php echo $media->getTitle(); ?>
                <?php elseif($media->getMediaTypeId()==2): ?>
                    <img src="<?php echo PWE::getBaseURL(); ?>admin/css/gfx/logoPdf.png" height="50" title="<?php echo $media->getTitle(); ?>" alt="<?php echo $media->getTitle(); ?>"/>
                    <?php echo $media->getTitle(); ?>
                <?php endif; ?>
            </li>
            <?php endforeach; ?>
        </ul>
    </div>
    <input type="hidden" id="findPermalienReturnTarget" value="" />
</div>

<!-- definir un menu -->
<div id="targetSidebar" title="Liste des rubriques">
    <ul class="selectableListe">
    <?php foreach(modele_category::getPrimaryListe() as $primary): ?>
        <li onclick="javascript:getPermalienFromListe($(this),'targetSidebar');" modele="category" modele_id="<?php echo $primary->getId(); ?>" permalien="<?php echo $primary->getPermalien()->getLien(); ?>"><?php echo $primary->getTitle(); ?></li>
        <?php if($primary->haveChild()): ?>
        <ul>
            <?php foreach($primary->getChild() as $secondary): ?>
                <li onclick="javascript:getPermalienFromListe($(this),'targetSidebar');" modele="category" modele_id="<?php echo $secondary->getId(); ?>" permalien="<?php echo $secondary->getPermalien()->getLien(); ?>"><?php echo $secondary->getTitle(); ?></li>
                <?php if($secondary->haveChild()): ?>
                <ul>
                    <?php foreach($secondary->getChild() as $third): ?>
                    <li onclick="javascript:getPermalienFromListe($(this),'targetSidebar');" modele="category" modele_id="<?php echo $third->getId(); ?>" permalien="<?php echo $third->getPermalien()->getLien(); ?>"><?php echo $third->getTitle(); ?></li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    <?php endforeach; ?>
    </ul>
    <input type="hidden" id="targetSidebarReturnTarget" value="" />
</div>